public class MySingleList implements IList{
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
//   链表属性，头节点
    public ListNode head;

    public void createList(){
//        node1.next = node2
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 =  new ListNode(45);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }
    @Override
    public void addFist(int data) {
        ListNode node = new ListNode(data);
        node.next   = head;
        this.head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
        }else {
            ListNode cur = head;
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }

    }

    @Override
    public void Index(int index, int data) {

        if (index < 0 || index >size()){
            throw new IndexException("index不合法："+index);
        }

        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            return;
        }
//        头插
        if (index == 0 ){
            addFist(data);
            return;
        }
//        尾插
        if (index == size()){
            addLast(data);
            return;
        }
//        中间插入
        ListNode cur = searchPreIndex(index);
        node.next = cur.next;
        cur.next = node;
    }
    private ListNode searchPreIndex(int index){
        int count = 0;
        ListNode cur = head;
        while (count != index-1){
            cur = cur.next;
            count ++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (head == null){
            return;
        }
        if(head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = findPreKey(key);
        if (cur == null){
//            没有你要删除的数字
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
    private ListNode findPreKey(int key){
        ListNode cur = head;
        while(cur.next != null){
            if (cur.next.val == key){
                return cur;
            }else {
                cur = cur.next;
            }
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if (head == null){
            return;
        }
//        前驱
        ListNode pre = head;
        ListNode cur = head.next;
//        遍历一次，将key全删除
        while(cur !=null){
            if(cur.val == key){
                pre.next = cur.next;
                cur = cur.next;
            }else {
                pre = cur;
                cur = cur.next;
            }
        }
//        除了头节点，其他都判断完了
        if (head.val == key){
            head = head.next;
        }
    }

//    求当前列表有多少个节点
    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count ++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
//        清空
        head = null;
    }

    @Override
    public void display() {
//        头结点不动
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }

//    判断回文
    public boolean chkPalindrome(){
        if (head == null){
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
//        找到中间节点
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
//        反转
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = cur.next;
//            一个往前，一个往后
            while(slow != head){
                if (slow.val != head.val){
                    return false;
                }
//                偶数情况
                if (head.next == slow){
                    return true;
                }
                slow = slow.next;
                head = head.next;
            }
        }
        return true;
    }

    public boolean cycle(){
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                return true;
            }
        }
        return false;
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pl = headA;
        ListNode ps = headB;

        int lenA = 0;
        int lenB = 0;

        while(pl != null){
            lenA++;
            pl = pl.next;
        }

        while(ps != null){
            lenB++;
            ps = ps.next;
        }

        pl = headA;
        ps = headB;

        int len = lenA - lenB;

        if(len<0){
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }

        while(len!=0){
            pl = pl.next;
            len--;
        }

//        考虑一直没有交点的情况
        while(pl != null && pl != ps){
            pl = pl.next;
            ps = ps.next;
        }

        if(pl == ps && pl == null){
            return null;
        }

        return pl;
    }

}
